﻿#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <algorithm>

using namespace std;
#include "vector.h"

void PrintVector(const vector<int>& v)
{
	vector<int>::const_iterator it1 = v.begin();
	while (it1 != v.end())
	{
		cout << *it1 << " ";
		it1++;
	}
	cout << endl;
}

template<class T>
void PrintVector(const vector<int>& v)
{
	typename vector<T>::const_iterator it1 = v.begin();
	while (it1 != v.end())
	{
		cout << *it1 << " ";
		it1++;
	}
	cout << endl;
}

void test_vector1()
{
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(4);

	v1[0] = 10;

	//遍历
	for (size_t i = 0; i < v1.size(); i++)
	{
		cout << v1[i] << " ";
	}
	cout << endl;

	//遍历2
	vector<int>::iterator it1 = v1.begin();
	while (it1 != v1.end())
	{
		cout << *it1 << " ";
		it1++;
	}
	cout << endl;

	//遍历3
	for(auto e:v1)
	{
		cout << e << " ";
	}
	cout << endl;
	PrintVector(v1);
}

void test_vector2()
{
	/*vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	v.push_back(5);

	v.insert(v.begin(), 0);
	for (auto e : v)
	{
		cout << e << " ";
	}
	cout << endl;

	v.erase(v.begin());
	for (auto e : v)
	{
		cout << e << " ";
	}
	cout << endl;*/


	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	v.push_back(5);
	v.push_back(6);

	//3的前面插入一个30
	//传迭代器区间，查找3
	vector<int>::iterator pos = find(v.begin(), v.end(), 3);
	if (pos != v.end())
	{
		v.insert(pos, 30);
	}
	for (auto e : v)
	{
		cout << e << " ";
	}
	cout << endl;

	//删除3
	//pos在insert以后就失效了，所以我们不要用他,重新找pos
	pos = find(v.begin(), v.end(), 3);
	if (pos != v.end())
	{
		v.erase(pos);
	}
	for (auto e : v)
	{
		cout << e << " ";
	}
	cout << endl;
}

void test_vector3()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	v.push_back(5);
	v.push_back(6);
	v.push_back(7);

	vector<int>::iterator it = v.begin();
	while (it != v.end())
	{
		if (*it % 2 == 0)
		{
			//erase(it)以后，it失效，不能++
			//erase 会返回删除位置it的下一个位置
			it = v.erase(it);
		}
		else
		{
			it++;
		}
	}
	for (auto e : v)
	{
		cout << e << " ";
	}
	cout << endl;
}

void test_vector7()
{
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(2);

	vector<int> v2;
	v2.push_back(10);
	v2.push_back(20);

	//c++98推荐使用第二个,c++11都一样
	swap(v1, v2);
	v1.swap(v2);

}

int main()
{
	//test_vector1();
	//test_vector2();
	test_vector3();
	return 0;
}